package cn.autumnorange.app.gateway.security;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler;
import org.springframework.security.web.savedrequest.HttpSessionRequestCache;
import org.springframework.security.web.savedrequest.RequestCache;
import org.springframework.security.web.savedrequest.SavedRequest;
import org.springframework.util.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class GateWaySavedRequestAwareAuthenticationSuccessHandler
    extends SimpleUrlAuthenticationSuccessHandler {
  protected final Log logger = LogFactory.getLog(this.getClass());
  private RequestCache requestCache = new HttpSessionRequestCache();

  public GateWaySavedRequestAwareAuthenticationSuccessHandler() {
    setRedirectStrategy(new GateWayDefaultRedirectStrategy());
  }

  public void onAuthenticationSuccess(
      HttpServletRequest request, HttpServletResponse response, Authentication authentication)
      throws ServletException, IOException {
    SavedRequest savedRequest = this.requestCache.getRequest(request, response);
    if (savedRequest == null) {
      super.onAuthenticationSuccess(request, response, authentication);
    } else {
      String targetUrlParameter = this.getTargetUrlParameter();
      if (!this.isAlwaysUseDefaultTargetUrl()
          && (targetUrlParameter == null
              || !StringUtils.hasText(request.getParameter(targetUrlParameter)))) {
        this.clearAuthenticationAttributes(request);
        String targetUrl = savedRequest.getRedirectUrl();
        this.logger.debug("Redirecting to DefaultSavedRequest Url: " + targetUrl);
        this.getRedirectStrategy().sendRedirect(request, response, targetUrl);
      } else {
        this.requestCache.removeRequest(request, response);
        super.onAuthenticationSuccess(request, response, authentication);
      }
    }
  }

  public void setRequestCache(RequestCache requestCache) {
    this.requestCache = requestCache;
  }
}
